package com.itxiuyixiu.homework.entity.dao;

import com.itxiuyixiu.homework.entity.model.Major;
import com.itxiuyixiu.homework.entity.vo.MajorVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄磊
 * @date 2020/3/5
 */
public interface MajorDao extends JpaRepository<Major, Integer> {
    Boolean existsByMajorName(String academyName);

    boolean existsByMajorNameAndAcademyId(String majorName , int academyId);

    boolean existsByAcademyId(int academyId);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, majorName, fk_academy_id as academyId from Major where id=?1" ,nativeQuery = true)
    List<MajorVo> findAllById(int id);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, majorName, fk_academy_id as academyId from Major where majorName=?1" ,nativeQuery = true)
    List<MajorVo> findAllByMajorName(String majorName);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, majorName, fk_academy_id as academyId from Major where fk_academy_id=?1" ,nativeQuery = true)
    List<MajorVo> findAllByAcademyId(int academyId);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, majorName, fk_academy_id as academyId from Major" ,nativeQuery = true)
    List<MajorVo> findAllMajor();
}
